<?php
define('CUR_PATH', dirname(__FILE__).DIRECTORY_SEPARATOR);
header('content-type:text/plain;charset=utf-8');
$pattern = '/[\x{4e00}-\x{9fa5}]+/u';
if (preg_match($pattern, $_GET['kw'], $m)) {// utf-8编码的参数
	$kw = urlencode($_GET['kw']);
} else if (preg_match('/[\x{a1}-\x{ff}]+/', $_GET['kw'], $m)) {// gbk编码的参数
	$kw = iconv('gbk', 'utf-8', $_GET['kw']);
	$kw = urlencode($kw);
} else {
	
	exit;
}
$url = 'http://toy.weather.com.cn/SearchBox/searchBox?callback=jsonp&_=1336038473033&language=zh&keyword='.$kw;
$json = getUrl($url);
$json = substr($json, 6, -2);
$arr = json_decode($json, true);
$cityCode = $arr['i'][0]['i'];
$kw = urldecode($kw);
// $url = 'http://www.weather.com.cn/weather/'.$cityCode.'.shtml';
$url = CUR_PATH.$cityCode.'.html';
$html = getUrl($url);
// echo strlen($html);
// exit;
$data = array();
$pattern = '~<h1 class="weatheH1">[\s]*'.$kw.'[\s\S]*\(([\d]{4}-[\d]{2}-[\d]{2})&nbsp;[\s]*(11:00)发布\)[\s\S]*?</h1>~is';
preg_match($pattern, $html, $m);
if (isset($m[1]) && isset($m[2])) {
	$data['date_time'] = $m[1].' '.$m[2];
}
$dateline = date('YmdH', strtotime($data['date_time']));


var_export($data);
exit;


function getUrl($url)
{
	return file_get_contents($url);
	$ch = curl_init();
	curl_setopt($ch, CURLOPT_URL, $url);
	curl_setopt($ch, CURLOPT_HEADER, 0);
	curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
	$content = curl_exec($ch);
	curl_close($ch);
	return $content;
}